﻿using System.Collections.Generic;
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI
{
	internal static class campaigns_Validator
	{
		// filters
		internal static List<Api_ValidatorMessage> filters_valid( Dictionary<string, object> filter_dict )
		{
			List<Api_ValidatorMessage> msg_list = new List<Api_ValidatorMessage>();
			Api_ValidatorMessage msg;

			// no filters specified
			if ( filter_dict.Count == 0 ) return msg_list;      // filters are optional

			// well hell, it ain't pretty but it works for now
			List<string> valid_filters = new List<string>();
			valid_filters.Add( "campaign_id" );
			valid_filters.Add( "list_id" );
			valid_filters.Add( "folder_id" );
			valid_filters.Add( "template_id" );
			valid_filters.Add( "status" );
			valid_filters.Add( "type" );
			valid_filters.Add( "from_name" );
			valid_filters.Add( "from_email" );
			valid_filters.Add( "title" );
			valid_filters.Add( "subject" );
			valid_filters.Add( "sendtime_start" );
			valid_filters.Add( "sendtime_end" );
			valid_filters.Add( "exact" );

			// if filters are specified, validate them
			// ----------------------------------------------------------------
			foreach ( KeyValuePair<string, object> filter in filter_dict )
			{
				// ---------------------------------------------- filter type checking
				if ( !valid_filters.Contains( filter.Key ) )
				{
					msg_list.Add( Validator.FormatMsg( EnumValues.MessageLevel.Error, "filter type",
						 string.Format( "campaigns: filter type '{0}' is not valid.", filter.Key ) ) );
				}
				// ---------------------------------------------- filter value exists
				if ( filter.Value == null || filter.Value.ToString() == "" )
				{
					msg_list.Add( Validator.FormatMsg( EnumValues.MessageLevel.Error, "filter value",
						 string.Format( "campaigns: value for filter '{0}' was not specified.", filter.Key ) ) );
				}
				// -------- value type checking
				switch ( filter.Key )
				{
					// strings
					case "campaign_id":
					case "list_id":
					case "from_name":
					case "from_email":
					case "title":
					case "subject":
						msg = Validator.parameter_type_valid( filter.Key, System.TypeCode.String.ToString(), filter.Value );
						if ( msg != null ) msg_list.Add( msg );
						break;
					// int
					case "folder_id":
					case "template_id":
						msg = Validator.parameter_type_valid( filter.Key, System.TypeCode.Int32.ToString(), filter.Value );
						if ( msg != null ) msg_list.Add( msg );
						break;
					// enums
					case "status":
						msg = Validator.parameter_type_valid( filter.Key, "campaign_status", filter.Value );
						if ( msg != null ) msg_list.Add( msg );
						break;
					case "type":
						msg = Validator.parameter_type_valid( filter.Key, "campaign_type", filter.Value );
						if ( msg != null ) msg_list.Add( msg );
						break;
					// date
					case "sendtime_start":
					case "sendtime_end":
						msg = Validator.parameter_type_valid( filter.Key, System.TypeCode.DateTime.ToString(), filter.Value );
						if ( msg != null ) msg_list.Add( msg );
						break;
					// bool
					case "exact":
						msg = Validator.parameter_type_valid( filter.Key, System.TypeCode.Boolean.ToString(), filter.Value );
						if ( msg != null ) msg_list.Add( msg );
						break;
				}
			}
			return msg_list;
		}
	}
}
